Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Text
Imports System.IO
Imports System.Globalization
Imports Microsoft.VisualBasic.CompilerServices
Public Class clsFilter
#Region "cboLookupFields"
    Public Class cboLookupFields
        Inherits System.Windows.Forms.ComboBox
        ' Methods
        Public Sub New()
            AddHandler MyBase.SelectedIndexChanged, New EventHandler(AddressOf Me.IndexChanged)
        End Sub
        Private Sub IndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        End Sub
    End Class
#End Region
#Region "FilterButton"
    Public Class FilterButton
        Inherits System.Windows.Forms.Button

#Region "Fields"
        Private cboLookup As ComboBox
        Private cboOpr As ComboBox
        Private dv As DataView
        Private strFields As String
        Private txtValue As TextBox
#End Region

#Region "Property"
        Public Property Dataview() As DataView
            Get
                Return Me.dv
            End Get
            Set(ByVal Value As DataView)
                Me.dv = Value
            End Set
        End Property

        Public Property Fields() As String
            Get
                Return Me.strFields
            End Get
            Set(ByVal Value As String)
                Me.strFields = Value
            End Set
        End Property
#End Region

#Region "Methods"
        Public Sub New(ByVal lctxtvalue As TextBox, ByVal lcdv As DataView, ByVal lcstrFields As String, ByVal lccboLookup As ComboBox, ByVal lccboOpr As ComboBox)
            AddHandler MyBase.Click, New EventHandler(AddressOf Me.Filter)
            Me.txtValue = lctxtvalue
            Me.dv = lcdv
            Me.strFields = lcstrFields
            Me.cboLookup = lccboLookup
            Me.cboOpr = lccboOpr
        End Sub
        Private Sub Filter(ByVal sender As Object, ByVal e As EventArgs)
            Try
                Dim strFilter As String, curField As String, curValue As String, cOpr As String
                curValue = Trim(txtValue.Text)
                If curValue = "" Then
                    SetFilterTo("", dv)
                    Return
                End If
                curField = Trim(clsSupport.GetWordNum(strFields, cboLookup.SelectedIndex + 1, ","))
                curField = Replace(curField, "~", "")
                cOpr = Trim(cboOpr.Text)
                Select Case dv.Table.Columns(curField).DataType.ToString()
                    Case "System.Char", "System.String"
                        Select Case Trim(cOpr)
                            Case "LIKE"
                                curValue = "'%" + curValue + "%'"
                            Case "="
                                cOpr = "LIKE"
                                curValue = "'" + curValue + "%'"
                            Case Else
                                curValue = "'" + curValue + "'"
                        End Select
                    Case "System.Byte", "System.SByte", "System.Int16", "System.Int32", "System.Int64", "System.Decimal", "System.Double", "System.Integer", "System.Long", "System.Sort", "System.Single"
                    Case "System.Boolean"
                    Case "System.Date", "System.DateTime"
                        curValue = "#" + Trim(clsSupport.GetWordNum(Trim(txtValue.Text), 2, "/")) + "/" + Trim(clsSupport.GetWordNum(Trim(txtValue.Text), 1, "/")) + "/" + Trim(clsSupport.GetWordNum(Trim(txtValue.Text), 3, "/")) + "#"
                End Select
                strFilter = curField + " " + cOpr + " " + curValue
                If txtValue.Tag = "1" Then
                    If Trim(dv.RowFilter) <> "" Then
                        strFilter = "(" + strFilter + ") AND (" + dv.RowFilter + ")"
                    End If
                End If
                SetFilterTo(strFilter, dv)
            Catch
            End Try
        End Sub

        Public Sub SetFilterTo(ByVal tcFilterExpression As String, ByVal toView As DataView)
            toView.RowFilter = tcFilterExpression
        End Sub
#End Region

    End Class
#End Region
#Region "FilterGridEngine"
    Public Class FilterGridEngine
        ' Methods
        Private Sub AddContextMenuItem()
            Dim contextMenu As New ContextMenu
            If Information.IsNothing(Me.Datagrid.ContextMenu) Then
                contextMenu = New ContextMenu
                Me.Datagrid.ContextMenu = contextMenu
            Else
                contextMenu = Me.Datagrid.ContextMenu
            End If
            Dim num2 As Integer = (contextMenu.MenuItems.Count - 1)
            Dim i As Integer = 0
            Do While (i <= num2)
                If (contextMenu.MenuItems.Item(i).Shortcut = Shortcut.CtrlL) Then
                    Return
                End If
                i += 1
            Loop
            Dim item As New MenuItem("Focus", New EventHandler(AddressOf Me.Show), Shortcut.CtrlL)
            item.Visible = False
            contextMenu.MenuItems.Add(item)
        End Sub

        Private Sub CboAutoChanged(ByVal sender As Object, ByVal e As EventArgs)
            Select Case Me.cboAuto.SelectedIndex
                Case 0
                    Me.txtFilter.Tag = ""
                    Me.AutoFilter = False
                    Exit Select
                Case 1
                    Me.txtFilter.Tag = "1"
                    Me.AutoFilter = False
                    Exit Select
                Case 2
                    Me.AutoFilter = True
                    Me.txtFilter.Tag = "2"
                    Exit Select
            End Select
            Me.txtFilter.Focus()
        End Sub

        Private Sub ContextMenuChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me.AddContextMenuItem()
        End Sub

        Private Function IsSubString(ByVal strParent As String, ByVal strChild As String, ByVal isCorrect As Boolean) As Boolean
            If Not isCorrect Then
                strParent = strParent.ToUpper
                strChild = strChild.ToUpper
            End If
            Dim length As Integer = strParent.Length
            Dim num2 As Integer = strChild.Length
            Dim num4 As Integer = (length - num2)
            Dim i As Integer = 0
            Do While (i <= num4)
                Dim sRight As String = strParent.Substring(i, num2)
                If (StringType.StrCmp(strChild, sRight, False) = 0) Then
                    Return True
                End If
                i += 1
            Loop
            Return False
        End Function

        Private Sub SearchGrid(ByRef Grid As DataGrid, ByVal strSearch As String)
            Dim columnNumber As Integer = Grid.CurrentCell.ColumnNumber
            If Not Information.IsNothing(Me.ComboBox) Then
                columnNumber = Me.ComboBox.SelectedIndex
            End If
            Dim num2 As Integer = 0
            Dim mappingName As String = Grid.TableStyles.Item(num2).GridColumnStyles.Item(columnNumber).MappingName
            Do While (1 <> 0)
                Try
                    num2 += 1
                    mappingName = Grid.TableStyles.Item(num2).GridColumnStyles.Item(columnNumber).MappingName
                    Continue Do
                Catch exception1 As Exception
                    Exit Do
                End Try
            Loop
            Try
                Dim dataSource As DataView = DirectCast(Grid.DataSource, DataView)
                If (StringType.StrCmp(dataSource.Table.Columns.Item(mappingName).DataType.ToString.Substring((dataSource.Table.Columns.Item(mappingName).DataType.ToString.LastIndexOf(".") + 1)), "String", False) = 0) Then
                    Dim str2 As String
                    Dim strParent As String = strSearch.Trim.Replace("%", "*")
                    Dim sColumnName As String = Strings.Trim(dataSource.Table.Columns.Item(mappingName).ColumnName.ToString)
                    If ((strParent.Length > 0) AndAlso Me.IsSubString(strParent, "*", False)) Then
                        str2 = Me.sFilter(sColumnName, strParent)
                    Else
                        str2 = (sColumnName & " LIKE '" & strParent & "*'")
                    End If
                    dataSource.RowFilter = str2
                End If
            Catch exception3 As Exception
            End Try
        End Sub

        Private Function sFilter(ByVal sColumnName As String, ByVal strSearch As String) As String
            Dim index As Integer() = New Integer((strSearch.Length + 1) - 1) {}
            Dim num2 As Integer = Me.StringCount(strSearch, "*", (index))
            Dim num As Integer = 0
            Do While (num < (num2 - 1))
                If (index(num) = (index((num + 1)) - 1)) Then
                    strSearch = strSearch.Remove(index(num), 1)
                    num = 0
                    num2 = Me.StringCount(strSearch, "*", (index))
                Else
                    num += 1
                End If
            Loop
            If (((num2 = 1) And ((StringType.StrCmp(strSearch.Substring(0, 1), "*", False) = 0) Or (StringType.StrCmp(strSearch.Substring((strSearch.Length - 1)), "*", False) = 0))) Or (((num2 = 2) And (StringType.StrCmp(strSearch.Substring(0, 1), "*", False) = 0)) And (StringType.StrCmp(strSearch.Substring((strSearch.Length - 1)), "*", False) = 0))) Then
                Return (sColumnName & " LIKE '" & strSearch & "'")
            End If
            Dim strParent As String = strSearch.Substring(1, (strSearch.Length - 2))
            num2 = Me.StringCount(strParent, "*", (index))
            Dim str3 As String = (sColumnName & " LIKE '" & strSearch.Substring(0, (index(0) + 2)) & "'")
            Dim num3 As Integer = (num2 - 1)
            num = 0
            Do While (num <= num3)
                If (num = (num2 - 1)) Then
                    str3 = String.Concat(New String() {str3, " AND ", sColumnName, " LIKE '", strSearch.Substring((index(num) + 1)), "'"})
                Else
                    str3 = String.Concat(New String() {str3, " AND ", sColumnName, " LIKE '", strSearch.Substring((index(num) + 1), ((index((num + 1)) - index(num)) + 1)), "'"})
                End If
                num += 1
            Loop
            Return str3
        End Function

        Private Sub Show(ByVal sender As Object, ByVal e As EventArgs)
            Try
                If Not Information.IsNothing(Me.ComboBox) Then
                    Me.ComboBox.SelectedIndex = Me.Datagrid.CurrentCell.ColumnNumber
                End If
                Me.txtFilter.Focus()
            Catch exception1 As Exception
            End Try
        End Sub

        Private Function StringCount(ByVal strParent As String, ByVal strChild As String, ByRef Index As Integer()) As Integer
            Dim length As Integer = strChild.Length
            Dim num3 As Integer = strParent.Length
            Dim xIndex As Integer = 0
            Dim num6 As Integer = (num3 - length)
            Dim i As Integer = 0
            Do While (i <= num6)
                Dim sRight As String = strParent.Substring(i, length)
                If (StringType.StrCmp(strChild, sRight, False) = 0) Then
                    Index(xIndex) = i
                    xIndex += 1
                End If
                i += 1
            Loop
            Return xIndex
        End Function

        Private Sub txtChanged(ByVal sender As Object, ByVal e As EventArgs)
            If (Me.lcisFiler AndAlso Not (Information.IsNothing(Me.lcGrid) Or Information.IsNothing(Me.txtFilter))) Then
                Me.SearchGrid((Me.lcGrid), StringType.FromObject(LateBinding.LateGet(sender, Nothing, "Text", New Object(0 - 1) {}, Nothing, Nothing)))
            End If
        End Sub


        ' Properties
        Public Property AutoFilter() As Boolean
            Get
                Return Me.lcisFiler
            End Get
            Set(ByVal Value As Boolean)
                Me.lcisFiler = Value
            End Set
        End Property

        Public Property ComboBox() As ComboBox
            Get
                Return Me.lcCbo
            End Get
            Set(ByVal Value As ComboBox)
                Me.lcCbo = Value
            End Set
        End Property

        Public Property Datagrid() As DataGrid
            Get
                Return Me.lcGrid
            End Get
            Set(ByVal Value As DataGrid)
                Me.lcGrid = Value
                Me.AddContextMenuItem()
                AddHandler Me.Datagrid.ContextMenuChanged, New EventHandler(AddressOf Me.ContextMenuChanged)
            End Set
        End Property

        Public ReadOnly Property Status() As Integer
            Get
                Return Me.cboAuto.SelectedIndex
            End Get
        End Property

        Public Property TextBox() As TextBox
            Get
                Return Me.txtFilter
            End Get
            Set(ByVal Value As TextBox)
                Me.txtFilter = Value
                AddHandler Me.txtFilter.TextChanged, New EventHandler(AddressOf Me.txtChanged)
                Me.txtFilter.Width = (Value.Width - 70)
                Me.cboAuto = New ComboBox
                Dim cboAuto As ComboBox = Me.cboAuto
                cboAuto.Items.Add("L" & ChrW(7885) & "c")
                cboAuto.Items.Add("L" & ChrW(7885) & "c ti" & ChrW(7871) & "p")
                cboAuto.Items.Add("T" & ChrW(7921) & " " & ChrW(273) & ChrW(7897) & "ng")
                cboAuto.TabIndex = Value.TabIndex
                cboAuto.TabStop = False
                cboAuto.SelectedIndex = 0
                cboAuto.DropDownStyle = ComboBoxStyle.DropDownList
                cboAuto.Top = Me.txtFilter.Top
                cboAuto.Width = 70
                cboAuto.Left = (Me.txtFilter.Left + Value.Width)
                cboAuto.Anchor = Me.txtFilter.Anchor
                cboAuto.Visible = True
                cboAuto = Nothing
                AddHandler Me.cboAuto.SelectedIndexChanged, New EventHandler(AddressOf Me.CboAutoChanged)
                Me.txtFilter.Parent.Controls.Add(Me.cboAuto)
            End Set
        End Property

        ' Fields
        Private cboAuto As ComboBox
        Private lcCbo As ComboBox
        Private lcGrid As DataGrid
        Private lcisFiler As Boolean = False
        Private txtFilter As TextBox
    End Class
#End Region
End Class
